package com.ds.dsm.view.config.grid;

import com.ds.common.JDSException;
import com.ds.config.ResultModel;
import com.ds.config.TreeListResultModel;
import com.ds.dsm.aggregation.api.method.APIMethodInfo;
import com.ds.dsm.aggregation.module.ModuleView;
import com.ds.dsm.view.config.grid.nav.GridConfigNavItems;
import com.ds.enums.db.MethodChinaName;
import com.ds.esd.custom.annotation.FormViewAnnotation;
import com.ds.esd.custom.annotation.nav.NavGroupViewAnnotation;
import com.ds.esd.custom.api.annotation.APIEventAnnotation;
import com.ds.esd.custom.bean.ApiClassConfig;
import com.ds.esd.custom.bean.MethodConfig;
import com.ds.esd.custom.enums.CustomMenuItem;
import com.ds.esd.custom.module.annotation.ModuleAnnotation;
import com.ds.esd.dsm.DSMFactory;
import com.ds.esd.tool.ui.enums.Dock;
import com.ds.esd.util.TreePageUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/dsm/view/config/grid/")
public class ViewGridNavService {


    @RequestMapping(method = RequestMethod.POST, value = "WinConfig")
    @FormViewAnnotation
    @ModuleAnnotation(imageClass = "spafont spa-icon-c-comboinput", caption = "面板配置")
    @APIEventAnnotation(autoRun = true, bindMenu = CustomMenuItem.treeNodeEditor)
    @ResponseBody
    public ResultModel<ModuleView> getWinConfig(String sourceClassName, String methodName, String domainId) {
        ResultModel<ModuleView> resultModel = new ResultModel();
        try {
            ApiClassConfig classConfig = DSMFactory.getInstance().getAggregationManager().getApiClassConfig(sourceClassName, domainId);
            if (classConfig != null) {
                MethodConfig methodAPIBean = classConfig.getMethodByName(methodName);
                resultModel.setData(new ModuleView(methodAPIBean.getModuleBean()));
            }

        } catch (JDSException e) {
            e.printStackTrace();
        }
        return resultModel;
    }

//
//    @MethodChinaName(cname = "API信息")
//    @RequestMapping(method = RequestMethod.POST, value = "APIMethodInfo")
//    @NavGroupViewAnnotation
//    @ModuleAnnotation(caption = "API信息", dock = Dock.fill, imageClass = "spafont spa-icon-c-grid")
//    @APIEventAnnotation(bindMenu = CustomMenuItem.treeNodeEditor)
//    @ResponseBody
//    public ResultModel<APIMethodInfo> getAPIMethodInfo(String domainId, String methodName, String sourceClassName) {
//        ResultModel<APIMethodInfo> result = new ResultModel<APIMethodInfo>();
//        return result;
//    }

    @RequestMapping(method = RequestMethod.POST, value = "ViewGridItemsConfig")
    @ModuleAnnotation(dynLoad = true, caption = "获取目录", imageClass = "bpmfont bpmgongzuoliuxitongpeizhi")
    @APIEventAnnotation(bindMenu = CustomMenuItem.loadChild)
    @ResponseBody
    public TreeListResultModel<List<GridNavTree>> getViewGridItemsConfig(String domainId, String sourceClassName, String methodName, String viewInstId) {
        TreeListResultModel<List<GridNavTree>> result = new TreeListResultModel<>();
        result = TreePageUtil.getTreeList(Arrays.asList(GridConfigNavItems.values()), GridNavTree.class);
        return result;
    }

}
